import { LinValidator, Rule } from "../code/lin-validator-v2"
import Menu from "../sql/module/menu"
import Role from "../sql/module/user/role"

export class AddRoleValidator extends LinValidator {
    constructor() {
        super()
        this.uid = [new Rule('isInt', '身份Id为0-100之间的整数', {
            max: 100,
            min: 0
        })]
        this.name = [new Rule('isLength', '身份名最多24位', {
            max: 24
        })]
    }
    async validateUid(vals: { body: { uid: any; id: any } }) {
        const uid = vals.body.uid
        const id = vals.body.id
        if (id) return
        const total = await Role.count({
            where: {
                uid
            }
        })
        if (total >= 1) {
            throw new Error('当前身份ID已存在')
        }
    }
    async validateMenu(vals: { body: { menus: any } }) {
        let menus = vals.body.menus
        try {
            if (!menus) {
                return
            }
            if (!Array.isArray(menus)) {
                throw new Error('权限格式不符合')
            }
        } catch (err) {
            throw new Error('权限格式异常')
        }
    }
}
export class MenusValidator extends LinValidator {
    constructor() {
        super()
        this.id = []
        this.label = [new Rule('isLength', '菜单名称最多6位', {
            max: 6
        })]
    }
}